<template>
  <a-modal
    title="编辑数据列"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-divider orientation="left">数据列信息</a-divider>
      <a-form :form="form">
        <a-form-item  v-show="false" >
          <a-input  v-decorator="['oldName']" />
        </a-form-item>
        <a-form-item  v-show="false" >
          <a-input  v-decorator="['tableName']" />
        </a-form-item>
        <a-form-item label="列名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入列名" v-decorator="['dbColumnName',{rules: [{required: true, message: '请输入列名！'}]}]" />
        </a-form-item>
        <a-form-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入描述" style="width: 100%" v-decorator="['columnDescription',{rules: [{required: true, message: '请输入描述！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    columnEdit
  } from '@/api/modular/gen/databaseManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        console.log(record);
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              oldName:record.dbColumnName,
              dbColumnName: record.dbColumnName,
              columnDescription:record.columnDescription,
              tableName:record.tableName
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            columnEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
